home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lisp / dabbrev.el < prev    next >
Lisp/Scheme  |  1993-07-23  |  11KB  |  281 lines

  1. ;;; dabbrev.el --- dynamic abbreviation package for GNU Emacs.
  2.  
  3. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  4.  
  5. ;; Last-Modified: 16 Mar 1992
  6. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  7.  
  8. ;; Maintainer: FSF
  9. ;; Keywords: abbrev
  10.  
  11. ;; This file is part of GNU Emacs.
  12.  
  13. ;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17.  
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;; GNU General Public License for more details.
  22.  
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  25. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27. ;;; Commentary:
  28.  
  29. ; DABBREVS - "Dynamic abbreviations" hack, originally written by Don Morrison
  30. ; for Twenex Emacs.  Converted to mlisp by Russ Fish.  Supports the table
  31. ; feature to avoid hitting the same expansion on re-expand, and the search
  32. ; size limit variable.  Bugs fixed from the Twenex version are flagged by
  33. ; comments starting with ;;; .
  34. ; converted to Emacs Lisp by Spencer Thomas.
  35. ; Thoroughly cleaned up by Richard Stallman.
  36. ;  
  37. ; If anyone feels like hacking at it, Bob Keller (Keller@Utah-20) first
  38. ; suggested the beast, and has some good ideas for its improvement, but
  39. ; doesn't know TECO (the lucky devil...).  One thing that should definitely
  40. ; be done is adding the ability to search some other buffer(s) if you can?t
  41. ; find the expansion you want in the current one.
  42.  
  43. ;;; Code:
  44.  
  45. ;; (defun dabbrevs-help ()
  46. ;;   "Give help about dabbrevs."
  47. ;;   (interactive)
  48. ;;   (&info "emacs" "dabbrevs")    ; Select the specific info node.
  49. ;; )
  50. (defvar dabbrevs-limit nil
  51.   "*Limits region searched by `dabbrevs-expand' to this many chars away.")
  52. (make-variable-buffer-local 'dabbrevs-limit)
  53.  
  54. (defvar dabbrevs-backward-only nil
  55.   "*If non-NIL, `dabbrevs-expand' only looks backwards.")
  56.  
  57. ; State vars for dabbrevs-re-expand.
  58. (defvar last-dabbrevs-table nil
  59.   "Table of expansions seen so far (local)")
  60. (make-variable-buffer-local 'last-dabbrevs-table)
  61.  
  62. (defvar last-dabbrevs-abbreviation ""
  63.   "Last string we tried to expand (local).")
  64. (make-variable-buffer-local 'last-dabbrevs-abbreviation)
  65.  
  66. (defvar last-dabbrevs-direction 0
  67.   "Direction of last dabbrevs search (local)")
  68. (make-variable-buffer-local 'last-dabbrevs-direction)
  69.  
  70. (defvar last-dabbrevs-abbrev-location nil
  71.   "Location last abbreviation began (local).")
  72. (make-variable-buffer-local 'last-dabbrevs-abbrev-location)
  73.  
  74. (defvar last-dabbrevs-expansion nil
  75.     "Last expansion of an abbreviation. (local)")
  76. (make-variable-buffer-local 'last-dabbrevs-expansion)
  77.  
  78. (defvar last-dabbrevs-expansion-location nil
  79.   "Location the last expansion was found. (local)")
  80. (make-variable-buffer-local 'last-dabbrevs-expansion-location)
  81.  
  82. ;;;###autoload
  83. (defun dabbrev-expand (arg)
  84.   "Expand previous word \"dynamically\".
  85. Expands to the most recent, preceding word for which this is a prefix.
  86. If no suitable preceding word is found, words following point are considered.
  87.  
  88. If `case-fold-search' and `case-replace' are non-nil (usually true)
  89. then the substituted word may be case-adjusted to match the abbreviation
  90. that you had typed.  This takes place if the substituted word, as found,
  91. is all lower case, or if it is at the beginning of a sentence and only
  92. its first letter was upper case.
  93.  
  94. A positive prefix arg N says to take the Nth backward DISTINCT
  95. possibility.  A negative argument says search forward.  The variable
  96. `dabbrev-backward-only' may be used to limit the direction of search to
  97. backward if set non-nil.
  98.  
  99. If the cursor has not moved from the end of the previous expansion and
  100. no argument is given, replace the previously-made expansion
  101. with the next possible expansion not yet tried."
  102.   (interactive "*P")
  103.   (let (abbrev expansion old which loc n pattern
  104.     (do-case (and case-fold-search case-replace)))
  105.     ;; abbrev -- the abbrev to expand
  106.     ;; expansion -- the expansion found (eventually) or nil until then
  107.     ;; old -- the text currently in the buffer
  108.     ;;    (the abbrev, or the previously-made expansion)
  109.     ;; loc -- place where expansion is found
  110.     ;;    (to start search there for next expansion if requested later)
  111.     ;; do-case -- non-nil if should transform case when substituting.
  112.     (save-excursion
  113.       (if (and (null arg)
  114.            (eq last-command this-command)
  115.            last-dabbrevs-abbrev-location)
  116.       (progn
  117.         (setq abbrev last-dabbrevs-abbreviation)
  118.         (setq old last-dabbrevs-expansion)
  119.         (setq which last-dabbrevs-direction))
  120.     (setq which (if (null arg)
  121.             (if dabbrevs-backward-only 1 0)
  122.                 (prefix-numeric-value arg)))
  123.     (setq loc (point))
  124.     (forward-word -1)
  125.     (setq last-dabbrevs-abbrev-location (point)) ; Original location.
  126.     (setq abbrev (buffer-substring (point) loc))
  127.     (setq old abbrev)
  128.     (setq last-dabbrevs-expansion-location nil)
  129.     (setq last-dabbrev-table nil))      ; Clear table of things seen.
  130.  
  131.       (setq pattern (concat "\\b" (regexp-quote abbrev) "\\(\\sw\\|\\s_\\)+"))
  132.       ;; Try looking backward unless inhibited.
  133.       (if (>= which 0)
  134.       (progn 
  135.         (setq n (max 1 which))
  136.         (if last-dabbrevs-expansion-location
  137.         (goto-char last-dabbrevs-expansion-location))
  138.         (while (and (> n 0)
  139.             (setq expansion (dabbrevs-search pattern t do-case)))
  140.           (setq loc (point-marker))
  141.           (setq last-dabbrev-table (cons expansion last-dabbrev-table))
  142.           (setq n (1- n)))
  143.         (or expansion
  144.         (setq last-dabbrevs-expansion-location nil))
  145.         (setq last-dabbrevs-direction (min 1 which))))
  146.  
  147.       (if (and (<= which 0) (not expansion)) ; Then look forward.
  148.       (progn 
  149.         (setq n (max 1 (- which)))
  150.         (if last-dabbrevs-expansion-location
  151.         (goto-char last-dabbrevs-expansion-location))
  152.         (while (and (> n 0)
  153.             (setq expansion (dabbrevs-search pattern nil do-case)))
  154.           (setq loc (point-marker))
  155.           (setq last-dabbrev-table (cons expansion last-dabbrev-table))
  156.           (setq n (1- n)))
  157.         (setq last-dabbrevs-direction -1))))
  158.  
  159.     (if (not expansion)
  160.     (let ((first (string= abbrev old)))
  161.       (setq last-dabbrevs-abbrev-location nil)
  162.       (if (not first)
  163.           (progn (undo-boundary)
  164.              (search-backward old)
  165.              (if (eq major-mode 'picture-mode)
  166.              (picture-replace-match abbrev t 'literal)
  167.                (replace-match abbrev t 'literal))))
  168.       (error (if first
  169.              "No dynamic expansion for \"%s\" found."
  170.              "No further dynamic expansions for \"%s\" found.")
  171.          abbrev))
  172.       ;; Success: stick it in and return.
  173.       (undo-boundary)
  174.       (search-backward old)
  175.       ;; Make case of replacement conform to case of abbreviation
  176.       ;; provided (1) that kind of thing is enabled in this buffer
  177.       ;; and (2) the replacement itself is all lower case.
  178.       ;; First put back the original abbreviation with its original
  179.       ;; case pattern.
  180.       (save-excursion
  181.     (if (eq major-mode 'picture-mode)
  182.         (picture-replace-match abbrev t 'literal)
  183.       (replace-match abbrev t 'literal)))
  184.       (search-forward abbrev)
  185.       (let ((do-case (and do-case
  186.               (string= (substring expansion 1)
  187.                    (downcase (substring expansion 1))))))
  188.     ;; First put back the original abbreviation with its original
  189.     ;; case pattern.
  190.     (save-excursion
  191.       (replace-match abbrev t 'literal))
  192. ;;; This used to be necessary, but no longer, 
  193. ;;; because now point is preserved correctly above.
  194. ;;;    (search-forward abbrev)
  195.     (if (eq major-mode 'picture-mode)
  196.         (picture-replace-match (if do-case (downcase expansion) expansion)
  197.                    (not do-case)
  198.                    'literal)
  199.       (replace-match (if do-case (downcase expansion) expansion)
  200.              (not do-case)
  201.              'literal)))
  202.       ;; Save state for re-expand.
  203.       (setq last-dabbrevs-abbreviation abbrev)
  204.       (setq last-dabbrevs-expansion expansion)
  205.       (setq last-dabbrevs-expansion-location loc))))
  206.  
  207. ;;;###autoload
  208. (define-key esc-map "/" 'dabbrev-expand)
  209.  
  210.  
  211. ;; Search function used by dabbrevs library.  
  212. ;; First arg is string to find as prefix of word.  Second arg is
  213. ;; t for reverse search, nil for forward.  Variable dabbrevs-limit
  214. ;; controls the maximum search region size.
  215.  
  216. ;; Table of expansions already seen is examined in buffer last-dabbrev-table,
  217. ;; so that only distinct possibilities are found by dabbrevs-re-expand.
  218. ;; Note that to prevent finding the abbrev itself it must have been
  219. ;; entered in the table.
  220.  
  221. ;; IGNORE-CASE non-nil means treat case as insignificant while
  222. ;; looking for a match and when comparing with previous matches.
  223. ;; Also if that's non-nil and the match is found at the beginning of a sentence
  224. ;; and is in lower case except for the initial
  225. ;; then it is converted to all lower case for return.
  226.  
  227. ;; Value is the expansion, or nil if not found.  After a successful
  228. ;; search, point is left right after the expansion found.
  229.  
  230. (defun dabbrevs-search (pattern reverse ignore-case)
  231.   (let (missing result (case-fold-search ignore-case))
  232.     (save-restriction         ; Uses restriction for limited searches.
  233.       (if dabbrevs-limit
  234.       (narrow-to-region last-dabbrevs-abbrev-location
  235.                 (+ (point)
  236.                    (* dabbrevs-limit (if reverse -1 1)))))
  237.       ;; Keep looking for a distinct expansion.
  238.       (setq result nil)
  239.       (setq missing nil)
  240.       (while  (and (not result) (not missing))
  241.     ; Look for it, leave loop if search fails.
  242.     (setq missing
  243.           (not (if reverse
  244.                (re-search-backward pattern nil t)
  245.                (re-search-forward pattern nil t))))
  246.  
  247.     (if (not missing)
  248.         (progn
  249.           (setq result (buffer-substring (match-beginning 0)
  250.                          (match-end 0)))
  251.           (let* ((test last-dabbrev-table))
  252.         (while (and test
  253.                 (not
  254.                  (if ignore-case
  255.                  (string= (downcase (car test))
  256.                       (downcase result))
  257.                    (string= (car test) result))))
  258.           (setq test (cdr test)))
  259.         (if test (setq result nil))))))    ; if already in table, ignore
  260.       (if result
  261.       (save-excursion
  262.         (let ((beg (match-beginning 0)))
  263.           (goto-char beg)
  264.           (and ignore-case
  265.            (string= (substring result 1)
  266.                 (downcase (substring result 1)))
  267.            (if (string= paragraph-start
  268.                 (concat "^$\\|" page-delimiter))
  269.                (and (re-search-backward sentence-end nil t)
  270.                 (= (match-end 0) beg))
  271.              (forward-char 1)
  272.              (backward-sentence)
  273.              (= (point) beg))
  274.            (setq result (downcase result))))))
  275.       result)))
  276.  
  277. (provide 'dabbrev)
  278.  
  279. ;;; dabbrev.el ends here
  280.